import Mock from 'mockjs'
// const Qs = require('qs')
const userDB = [
  { username: 'admin', password: 'admin', id: '1', role: 'admin', token: '1', tokenHead: 'Bearer' },
  { username: 'editor', password: 'editor', id: '2', role: 'admin', token: '2', tokenHead: 'Bearer' },
  { username: 'user1', password: 'user1', id: '3', role: 'user', token: '3', tokenHead: 'Bearer' },
  { username: 'user2', password: 'user2', id: '4', role: 'user', token: '4', tokenHead: 'Bearer' }
]
Mock.mock('/api/userInfo', 'post', ({ body }) => {
  const bodyObj = JSON.parse(body)
  if (!bodyObj.token) {
    return {
      status: 401,
      msg: 'token错误，需要登录',
      data: {}
    }
  }
  const user = userDB.find(e => e.id === bodyObj.id)
  if (user) {
    return {
      status: 0,
      msg: '成功',
      data: {
        username: user.username,
        id: user.id
        // permissions: permissions[user.role]
      }
    }
  } else {
    return {
      status: 401,
      msg: '用户名或密码错误',
      data: {}
    }
  }
})

export default [{
  path: '/api/login',
  method: 'post',
  handle ({ body }) {
    const user = userDB.find(e => e.username === body.username && e.password === body.password)
    if (user) {
      return {
        code: 200,
        msg: '登录成功',
        data: {
          ...user
        }
      }
    } else {
      return {
        code: 401,
        msg: '用户名或密码错误',
        data: {}
      }
    }
  }
}]
